package com.my.mvvmrapid.ui.main.fragment.homeshop;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.FragmentHomeShopBinding;
import com.my.mvvmrapid.ui.main.fragment.homeshop.adapter.HomeShopFragmentStateAdapter;
import com.my.mvvmrapid.ui.main.fragment.homeshop.adapter.HomeShopNavigatorAdapter;
import com.my.mvvmrapid.ui.main.fragment.homeshop.shoptest.HomeShopTestFragment;
import com.my.mvvmrapid.view.HomeShopScrollView;
import com.zhongjh.mvvmrapid.base.ui.BaseFragment;
import com.zhongjh.mvvmrapid.utils.ToastUtils;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhongjh on 2021/4/12.
 */
public class HomeShopFragment extends BaseFragment<FragmentHomeShopBinding, HomeShopViewModel> {

    public static HomeShopFragment newInstance() {
        return new HomeShopFragment();
    }

    @Override
    public int initContentView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_shop;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {

    }

    @Override
    public void initData() {
        initHomeShopScrollView();
        initMagicIndicator();
    }

    @Override
    public void initViewObservable() {
        binding.homeScrollView.setOnPullListener(new HomeShopScrollView.OnPullListener() {
            @Override
            public void onPull(int alpha) {
                binding.sbView.getBackground().mutate().setAlpha(alpha);
                binding.vTop.getBackground().mutate().setAlpha(alpha);
                binding.vSearch.getBackground().mutate().setAlpha(alpha);
            }

            @Override
            public void onRefresh() {
                new Handler().postDelayed(() -> {
                    ToastUtils.showShort("刷新完成");
                    binding.homeScrollView.refreshFinish();
                }, 1500);
            }
        });

        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                // 只有第一个才支持全局刷新
                binding.homeScrollView.setDisable(position != 0);
            }
        });
    }

    private void initHomeShopScrollView() {
        binding.homeScrollView.setView(binding.viewPager2, binding.llContent, binding.imgHomeShopTop, binding.marginSpacer,
                binding.tvRefreshState, binding.magicIndicator);
    }

    private void initMagicIndicator() {
        List<String> listTitle = new ArrayList<>();
        List<Fragment> listFragments = new ArrayList<>();

        listTitle.add("首页");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("手机");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("男装");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("电脑办公");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("生鲜");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("数码");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("医药健康");
        listFragments.add(HomeShopTestFragment.newInstance());

        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        HomeShopNavigatorAdapter commonNavigatorAdapter = new HomeShopNavigatorAdapter(listTitle, binding.viewPager2);
        commonNavigator.setAdapter(commonNavigatorAdapter);
        commonNavigator.setLeftPadding(UIUtil.dip2px(getContext(), 10));
        commonNavigator.setRightPadding(UIUtil.dip2px(getContext(), 15));
        binding.magicIndicator.setNavigator(commonNavigator);

        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                binding.magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                binding.magicIndicator.onPageScrollStateChanged(state);
            }
        });

        HomeShopFragmentStateAdapter homeFragmentPageAdapter = new HomeShopFragmentStateAdapter(getChildFragmentManager(), getLifecycle());
        binding.viewPager2.setAdapter(homeFragmentPageAdapter);
        binding.viewPager2.setOffscreenPageLimit(listFragments.size());
    }
}
